package com.sx.micowell.wxapi;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.Toast;

import com.example.mvvm.rx.RxBus;
import com.example.mvvm.until.LogUtils;
import com.example.mvvm.until.Utils;
import com.sx.micowell.framework.app.AppConstant;
import com.sx.micowell.framework.tools.EventInfo;
import com.sx.micowell.framework.tools.wxapi.WxEntryRespEvent;
import com.tencent.mm.opensdk.diffdev.DiffDevOAuthFactory;
import com.tencent.mm.opensdk.diffdev.IDiffDevOAuth;
import com.tencent.mm.opensdk.diffdev.OAuthErrCode;
import com.tencent.mm.opensdk.diffdev.OAuthListener;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

/**
 * @Author: YY
 * @Time: 2021/9/11
 * @Describe 微信登录、支付
 */
public class WxUtils {

    private final IWXAPI iwxapi;

    public WxUtils() {

        iwxapi = WXAPIFactory.createWXAPI(Utils.getContext(), null);
        // 将该app注册到微信
        iwxapi.registerApp(AppConstant.WX_APP_ID);
    }

    public void pay(String content) {
        LogUtils.d("pay: " + content);
        if (TextUtils.isEmpty(content)) {
            return;
        }
        JSONObject json;
        try {
            json = new JSONObject(content);
            if (!json.has("retcode")) {
                PayReq req = new PayReq();
                req.appId = json.getString("appId");
                req.partnerId = json.getString("partnerId");
                req.prepayId = json.getString("prepayId");
                req.nonceStr = json.getString("nonceStr");
                req.timeStamp = json.getString("timeStamp");
                req.packageValue = json.getString("packageValue");
                req.sign = json.getString("sign");
                req.extData = "app data"; // optional
                // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
                iwxapi.sendReq(req);
            } else {
                LogUtils.e("返回错误" + json.getString("retmsg"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public void auth() {
        //先判断用户手机是否安装了微信客户端
        if (!iwxapi.isWXAppInstalled()) {
            Toast.makeText(Utils.getContext(), "您的设备未安装微信客户端", Toast.LENGTH_SHORT).show();
        } else {
            //向微信开放平台发起请求
            final SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_sdk_demo_test";
            iwxapi.sendReq(req);
        }
    }

    //判断是否安装了微信
    public boolean isWeiXinAvailable() {
        return iwxapi.isWXAppInstalled();
    }

    public void qrAuth(String appId, String scope, String noncestr, String timestamp, String signature ) {
        IDiffDevOAuth iDiffDevOAuth = DiffDevOAuthFactory.getDiffDevOAuth();
        iDiffDevOAuth.auth(appId, scope, noncestr, timestamp, signature, new OAuthListener() {
            @Override
            public void onAuthGotQrcode(String s, byte[] bytes) {
                // TODO: auth之后返回的二维码接口
                if (bytes != null) {
                    LogUtils.d("onAuthGotQrcode: "+s + "==== "+ Arrays.toString(bytes));
                    RxBus.getDefault().post(new WxEntryRespEvent(AppConstant.EVENT_WX_SCAN_CODE, 0, "", bytes));
                }
            }

            @Override
            public void onQrcodeScanned() {
                // TODO: 用户扫描二维码之后，回调改接口
                LogUtils.d("onQrcodeScanned");
            }

            @Override
            public void onAuthFinish(OAuthErrCode oAuthErrCode, String s) {
                // TODO: 用户点击授权后，回调改接口
                LogUtils.d("onAuthFinish: "+oAuthErrCode.getCode() + "--- "+ s);
                RxBus.getDefault().post(new WxEntryRespEvent(oAuthErrCode.getCode(), 0, s, null));
            }
        });
    }

}
